package vocabularyhub.io.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import vocabularyhub.io.model.ISimplestReader;
import vocabularyhub.word.impl.SimplestWord;
import vocabularyhub.word.model.IWord;

/**
 * 
 * @author Leon
 * 
 */
public class SimplestReader implements ISimplestReader {
    static Logger logger = Logger.getLogger(SimplestReader.class);

    @Override
    public List<IWord> parseWordListFile(File f) throws FileNotFoundException {
        List<IWord> words = new LinkedList<IWord>();

        BufferedReader reader = new BufferedReader(new FileReader(f));
        String line;
        int count = 0;
        try {

            while ((line = reader.readLine()) != null) {
                if (line.startsWith("#")) {
                    continue;
                }
                count++;
                words.add(new SimplestWord(line.trim()));
            }
        } catch (IllegalStateException e) {
            logger.warn(e.getMessage());
        } catch (IOException e) {
            logger.warn(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
        }
        logger.debug(String.format("%d words read.", count));

        return words;

    }
}
